package com.amiu.spring.config;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.core.env.Environment;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
//启用注解事务管理，使用CGLib代理 
@EnableTransactionManagement(proxyTargetClass = true)
//加载资源文件  
@PropertySource({"classpath:dbConfig.properties"}) 
@MapperScan(basePackages={"com.amiu.autoMybatis.mapper","com.amiu.shiro.db"})
public class MybatisConfig {
	
	//临时用
//	String propsDriverKey="db.driver";
//	String propsUrlKey ="db.shiro_test.url";
//	String propsUsernameKey="db.username";
//	String propsPasswordKey="db.password";
	@Autowired Environment env;
	//绑定资源属性 
	@Value("${db.driver}")
	String driver;
	@Value("${db.shiro_test.url}")
	String url;
	@Value("${db.username}")
	String username;
	@Value("${db.password}")
	String password;
	
	@Bean
	public DruidDataSource druidDataSource() throws IOException{
//		注释掉的是使用propertiesClasspathReader()方法获取properties的
//		Properties props = null;
//		try {
//			props = propertiesClasspathReader("dbConfig.properties");
//		} catch(IOException e) {
//			e.printStackTrace();
//		}
//		String driver = props.getProperty(propsDriverKey);
//		String url = props.getProperty(propsUrlKey);
//		String username = props.getProperty(propsUsernameKey);
//		String password = props.getProperty(propsPasswordKey);
		DruidDataSource druidDataSource = new DruidDataSource();
		//username = env.getProperty("username");
		druidDataSource.setDriverClassName(driver);
		druidDataSource.setUrl(url);
		druidDataSource.setUsername(username);
		druidDataSource.setPassword(password);
		return druidDataSource;
	}
	
	@Bean
	public DataSourceTransactionManager transactionManager() throws IOException{
		DataSourceTransactionManager transactionManager = 
				new DataSourceTransactionManager(druidDataSource());
		return transactionManager;
	}
	
	@Bean
	public SqlSessionFactoryBean sqlSessionFactory() throws IOException{
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(druidDataSource());
		sqlSessionFactoryBean.setTypeAliasesPackage("com.amiu");
//		sqlSessionFactoryBean.setMapperLocations(
//        		new PathMatchingResourcePatternResolver()
//        			.getResources("classpath:mapper/*.xml"));
		return sqlSessionFactoryBean;
	}
	//上面的@PropertySource可以加载properties
	private Properties propertiesClasspathReader(String classPathName)
			throws IOException, FileNotFoundException {
		//Spring提供的properties读取工具类
		Properties props = PropertiesLoaderUtils.loadAllProperties(classPathName);
		//下面是自己写的，有点小问题
//		Properties props = new Properties();
//		String fileName = classPathName.substring(classPathName.indexOf(":")+1);
//		InputStream is = MybatisInitialization.class.getResourceAsStream(fileName);
//		ClassLoader.getSystemResource(fileName);
//		props.load(is);
		return props;
	}
	
//	@Bean
//	public PropertyPlaceholderConfigurer propertyConfigurer(){
//		PropertyPlaceholderConfigurer propertyConfigurer = new PropertyPlaceholderConfigurer();
//		propertyConfigurer.setProperties(new Properties());
//		return propertyConfigurer;
//	}
}
